/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayerInitPanel.java
 *
 * Created on Jul 29, 2011, 1:51:33 PM
 */
package deatax.display.gui;

import deatax.network.Client;
import deatax.network.Messages;
import deatax.network.Server;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;

/**
 *
 * @author pson
 */
public class HotSeatInitPanel extends javax.swing.JPanel {

    private int n;
    Server s = null;

    /** Creates new form PlayerInitPanel */
    public HotSeatInitPanel() {
        initComponents();
        pnlPlayers.add(new PlayerCreatePanel(1));
        pnlPlayers.add(new PlayerCreatePanel(2));
        n = 3;
    }

    public void open() {
        if (s != null) {
            s.kill();
        }
        try {
            s = new Server(9999);
            s.run();
            Client.get().connect("localhost", 9999);
        } catch (Exception ex) {
        }
    }
    
    public void close() {
    }

    public void assignPlayers() {
        ArrayList<String> args = new ArrayList<String>();
        for (Component c : pnlPlayers.getComponents()) {
            if (c instanceof PlayerCreatePanel) {
                PlayerCreatePanel pcp = (PlayerCreatePanel) c;
                //Client.get().getControl().addPlayer(pcp.getPlayerName(), pcp.getColor());
                args.add(pcp.getPlayerName());
                args.add(String.valueOf(pcp.getColor().getRGB()));
            }
        }
        Server.get().sendMessages(Messages.PLAYER_SET, args);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAdd = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        pnlPlayers = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        btnAdd.setText("Add Player");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        pnlPlayers.setLayout(new javax.swing.BoxLayout(pnlPlayers, javax.swing.BoxLayout.Y_AXIS));
        jScrollPane1.setViewportView(pnlPlayers);

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+9));
        jLabel1.setText("Hotseat");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                    .add(btnAdd))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                .add(7, 7, 7)
                .add(btnAdd)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        pnlPlayers.add(new PlayerCreatePanel(n));
        n++;
        validate();
    }//GEN-LAST:event_btnAddActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlPlayers;
    // End of variables declaration//GEN-END:variables


}
